﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class SalesOrderList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //  UART.BLL.Sales.SalesOrderHeader bll = new UART.BLL.Sales.SalesOrderHeader();
                //设置经办人
                Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                //  UART.BLL.Accounts.Accounts_Department bllDept = new UART.BLL.Accounts.Accounts_Department();
                var v = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = v.Where(h => h.ClassID.ToString() == currentUser.DepartmentID).FirstOrDefault().RootID.ToString();
                this.lblStrWhere.Text = " RootDepartmentID=" + this.lblRootDepartmentID.Text;
                var b = mk.l.Sales_SalesOrderHeader.ToList();
                // this.AspNetPager1.RecordCount = b.Where(h=>h.RootDepartmentID==int.Parse(this.lblRootDepartmentID.Text)).Count();
                this.BindData();
                //设置分页页数
              //  this.txtPageSize.Attributes.InsertOnSubmit("onkeydown", "javascript:SetFocusPageSize();");

            }
        }
        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            this.BindData();
        }

        //protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        //显示客户名称
        //        Label lblCustomerName = (Label)(e.Row.FindControl("lblCustomerName"));
        //        //实例化客户业务逻辑处理类
        //        Sales_Customer bllC = new Sales_Customer();
        //        //得到Customer实体类
        //        UART.Model.Sales.Customer mC = bllC.GetModel
        //            (int.Parse(this.myGridView.DataKeys
        //            [e.Row.RowIndex].Values["CustomerID"].ToString()));
        //        if (mC != null)
        //        {   //显示客户的名称
        //            lblCustomerName.Text = mC.CustomerName;
        //        }
        //        //显示设计师姓名
        //        Label lblDesigner = (Label)(e.Row.FindControl("lblDesigner"));
        //        UART.BLL.Accounts.Accounts_Users bllUser = new UART.BLL.Accounts.Accounts_Users();
        //        UART.Model.Accounts.Accounts_Users mUser = bllUser.GetModel
        //            (this.myGridView.DataKeys
        //            [e.Row.RowIndex].Values["Designer"].ToString());
        //        lblDesigner.Text = mUser.TrueName;
        //        //显示提交状态
        //        Label lblStatusSubmit = (Label)(e.Row.FindControl("lblStatusSubmit"));
        //        string strStatusSubmit = this.myGridView.DataKeys
        //            [e.Row.RowIndex].Values["StatusSubmit"].ToString();
        //        switch (strStatusSubmit)
        //        {
        //            case "1":
        //                lblStatusSubmit.Text = "初始添加";
        //                lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
        //                break;
        //            case "2":
        //                lblStatusSubmit.Text = "修改中";
        //                lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
        //                break;
        //            case "3":
        //                lblStatusSubmit.Text = "已提交";
        //                lblStatusSubmit.ForeColor = System.Drawing.Color.Green;
        //                break;
        //            default:
        //                lblStatusSubmit.Text = "未知";
        //                lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
        //                break;
        //        }

        //        //显示付款状态
        //        Label lblStatusPayment = (Label)(e.Row.FindControl("lblStatusPayment"));
        //        string strStatusPayment = this.myGridView.DataKeys
        //            [e.Row.RowIndex].Values["StatusPayment"].ToString();
        //        switch (strStatusPayment)
        //        {
        //            case "1":
        //                lblStatusPayment.Text = "未付款";
        //                lblStatusPayment.ForeColor = System.Drawing.Color.Red;
        //                break;
        //            case "2":
        //                lblStatusPayment.Text = "未付全款";
        //                lblStatusPayment.ForeColor = System.Drawing.Color.Black;
        //                break;
        //            case "3":
        //                lblStatusPayment.Text = "已付全款";
        //                lblStatusPayment.ForeColor = System.Drawing.Color.Green;
        //                break;
        //            default:
        //                lblStatusPayment.Text = "未知";
        //                lblStatusPayment.ForeColor = System.Drawing.Color.Red;
        //                break;
        //        }

        //        //显示订货状态
        //        Label lblStatusPurchaseMaterial =
        //            (Label)(e.Row.FindControl("lblStatusPurchaseMaterial"));
        //        string strStatusPurchaseMaterial =
        //            this.myGridView.DataKeys[e.Row.RowIndex].
        //            Values["StatusPurchaseMaterial"].ToString();
        //        switch (strStatusPurchaseMaterial)
        //        {
        //            case "1":
        //                lblStatusPurchaseMaterial.Text = "未订货";
        //                lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
        //                break;
        //            case "2":
        //                lblStatusPurchaseMaterial.Text = "部分订货";
        //                lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
        //                break;
        //            case "3":
        //                lblStatusPurchaseMaterial.Text = "已订货";
        //                lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Green;
        //                break;
        //            default:
        //                lblStatusPurchaseMaterial.Text = "未知";
        //                lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
        //                break;
        //        }

        //        //显示流程状态
        //        Label lblStatusAll = (Label)(e.Row.FindControl("lblStatusAll"));
        //        string strStatusAll = this.myGridView.DataKeys[e.Row.RowIndex].
        //            Values["StatusAll"].ToString();
        //        switch (strStatusAll)
        //        {
        //            case "1":
        //                lblStatusAll.Text = "未完成";
        //                lblStatusAll.ForeColor = System.Drawing.Color.Red;
        //                break;
        //            case "2":
        //                lblStatusAll.Text = "已完成";
        //                lblStatusAll.ForeColor = System.Drawing.Color.Green;
        //                break;
        //            default:
        //                lblStatusAll.Text = "未知";
        //                lblStatusAll.ForeColor = System.Drawing.Color.Red;
        //                break;
        //        }
        //    }
        //}
        private void BindData()
        {
            Sales_SalesOrderHeader bll = new Sales_SalesOrderHeader();
            //  DataSet ds = bll.GetList();
            var n = mk.l.Sales_SalesOrderHeader.ToList();

            // var ds = mk.l.GetRecordFromPage("Sales_SalesOrderHeader", "SalesOrderID", this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, true, true, "1=1");
            this.myGridView.DataSource = n;
            this.myGridView.DataBind();
        }
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //显示客户名称
                Label lblCustomerName = (Label)(e.Row.FindControl("lblCustomerName"));
                //实例化客户业务逻辑处理类
                Sales_Customer bllC = new Sales_Customer();
                //得到Customer实体类
                var c = mk.l.Sales_Customer.ToList();
                Sales_Customer mC = c.Where(h => h.CustomerID ==
                    (int.Parse(this.myGridView.DataKeys
                    [e.Row.RowIndex].Values["CustomerID"].ToString()))).FirstOrDefault();
                if (mC != null)
                {   //显示客户的名称
                    lblCustomerName.Text = mC.CustomerName;
                }
                //显示设计师姓名
                Label lblDesigner = (Label)(e.Row.FindControl("lblDesigner"));
                var x = mk.l.Accounts_Users.ToList();
                //  UART.BLL.Accounts.Accounts_Users bllUser = new UART.BLL.Accounts.Accounts_Users();
                Accounts_Users mUser = x.Where(h => h.UserName ==
                    (this.myGridView.DataKeys
                    [e.Row.RowIndex].Values["Designer"].ToString())).FirstOrDefault();
                if (mUser != null)
                {
                    lblDesigner.Text = mUser.TrueName;
                }

                //显示提交状态
                Label lblStatusSubmit = (Label)(e.Row.FindControl("lblStatusSubmit"));
                string strStatusSubmit = this.myGridView.DataKeys
                    [e.Row.RowIndex].Values["StatusSubmit"].ToString();
                switch (strStatusSubmit)
                {
                    case "1":
                        lblStatusSubmit.Text = "初始添加";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusSubmit.Text = "修改中";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "3":
                        lblStatusSubmit.Text = "已提交";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusSubmit.Text = "未知";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                //显示付款状态
                Label lblStatusPayment = (Label)(e.Row.FindControl("lblStatusPayment"));
                string strStatusPayment = this.myGridView.DataKeys
                    [e.Row.RowIndex].Values["StatusPayment"].ToString();
                switch (strStatusPayment)
                {
                    case "1":
                        lblStatusPayment.Text = "未付款";
                        lblStatusPayment.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusPayment.Text = "未付全款";
                        lblStatusPayment.ForeColor = System.Drawing.Color.Black;
                        break;
                    case "3":
                        lblStatusPayment.Text = "已付全款";
                        lblStatusPayment.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusPayment.Text = "未知";
                        lblStatusPayment.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                //显示订货状态
                Label lblStatusPurchaseMaterial =
                    (Label)(e.Row.FindControl("lblStatusPurchaseMaterial"));
                string strStatusPurchaseMaterial =
                    this.myGridView.DataKeys[e.Row.RowIndex].
                    Values["StatusPurchaseMaterial"].ToString();
                switch (strStatusPurchaseMaterial)
                {
                    case "1":
                        lblStatusPurchaseMaterial.Text = "未订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusPurchaseMaterial.Text = "部分订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "3":
                        lblStatusPurchaseMaterial.Text = "已订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusPurchaseMaterial.Text = "未知";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                //显示流程状态
                Label lblStatusAll = (Label)(e.Row.FindControl("lblStatusAll"));
                string strStatusAll = this.myGridView.DataKeys[e.Row.RowIndex].
                    Values["StatusAll"].ToString();
                switch (strStatusAll)
                {
                    case "1":
                        lblStatusAll.Text = "未完成";
                        lblStatusAll.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusAll.Text = "已完成";
                        lblStatusAll.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusAll.Text = "未知";
                        lblStatusAll.ForeColor = System.Drawing.Color.Red;
                        break;
                }
            }
        }

        protected void btnSetPageSize_Click(object sender, EventArgs e)
        {
            if (this.txtPageSize.Text.Trim() != "")
            {
                if (this.txtPageSize.Text.Trim() != "0")
                {
                    //   this.AspNetPager1.PageSize = int.Parse(this.txtPageSize.Text.Trim());
                    this.BindData();
                }
                else
                {
                    //   this.AspNetPager1.PageSize = 10;
                    this.BindData();
                }
            }
            else
            {
                // this.AspNetPager1.PageSize = 10;
                this.BindData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void myGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.myGridView.PageIndex = e.NewPageIndex;
           // this.myGridView.DataBind();
            this.BindData();
        }
    }
}